/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package suncertify.shared;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import suncertify.ui.ClientProperty;
import suncertify.ui.ClientTest;


/**
 *
 * @author jshah
 * Event Handler class for <code>ClientConfigUI</code>
 */
public class DBConfigController implements ActionListener{

    DBConfigUI instance ;
    ClientProperty propertyClass;
    Properties props;

    /**
     * Default constructor for <code>DBConfigController</code> to handle events from the <code>DBConfigUI</code> screen
     */
    public DBConfigController(){
     
        try {
            props = new Properties();
            props.load(new FileInputStream(ServerProperty.getPropFile()));
        } catch (FileNotFoundException ex) {
            Logger.getLogger(DBConfigController.class.getName()).log(Level.SEVERE, null, ex);
        }catch (IOException ex) {
            Logger.getLogger(DBConfigController.class.getName()).log(Level.SEVERE, null, ex);
        } 
    }
    /**
     * sets a <code>DBConfigUI</code> instance so that the current controller can access the components of <code>DBConfigUI</code>
     * @param instance
     */
    public void setInstance(DBConfigUI instance) {
        this.instance = instance;
    }

    public void actionPerformed(ActionEvent e) {

        if(e.getSource()== this.instance.getSubmit()){

            String host = this.instance.getLocation().getText();
           

            if(!this.validateInput(host)){
                JOptionPane.showMessageDialog(instance.getClientConfigScreen().getContentPane(), "INVALID ENTRY");
            }

            else{
                 this.instance.getClientConfigScreen().setVisible(false);
                 this.instance.getClientConfigScreen().dispose();
                 String temploc = this.instance.getLocation().getText();
                 DefaultConfig.DBLOCATION = temploc;
                 this.setDBLocation(temploc);
                 this.switchScreen();
            }

            
        }

        if(e.getSource()== this.instance.getChoosefile()){

            JFileChooser fchoose = new JFileChooser();
            int f = fchoose.showDialog(this.instance.getClientConfigScreen(), "OPEN DB FILE");

            if(f == JFileChooser.APPROVE_OPTION){
                try {
                    File file = fchoose.getSelectedFile();
                    
                    Logger.getLogger(DBConfigController.class.getName()).log(Level.INFO, "{0}\n{1}\n{2}\n{3}\n{4}", new Object[]{file.getName(), file.getCanonicalPath(), file.getAbsolutePath(), file.getParent(), file.getPath()});
                    int response = JOptionPane.showConfirmDialog(this.instance.clientConfigScreen, "DB LOCATION:"+ file.getAbsolutePath(),"CONFIRM DB LOCATION", JOptionPane.OK_CANCEL_OPTION);
                    if(response == JOptionPane.OK_OPTION){
                        String temploc = file.getAbsolutePath();
                        DefaultConfig.DBLOCATION = temploc;
                        
                        this.setDBLocation(temploc);
                        this.switchScreen();
                    }
                } catch (IOException ex) {
                    Logger.getLogger(DBConfigController.class.getName()).log(Level.SEVERE, "FILE NOT ACCESIBLE", ex);
                    JOptionPane.showConfirmDialog(this.instance.clientConfigScreen, "DB FILE INVALID PLEASE TRY AGAIN");
                }
            }
        }

        if(e.getSource() == this.instance.getDefaultlocation()){

            //if default option is selected, use the location as null so no new location is set and previous location is used
            this.setDBLocation(null);
            System.out.println( "SHOULD SWITCH SCREEN");
            this.switchScreen();
        }

    }

    private void switchScreen(){

        this.instance.clientConfigScreen.setVisible(false);
        ClientTest.showCSR = true;

    }

    private boolean validateInput(String host){

        if(host==null || host.isEmpty())
            return false;
        return true;

    }

    /**
     *
     * @param location
     */
    public  void setDBLocation(String location){

        //if defualt location then the location is null so the previously set DB properties will be used
        if(!(location == null))
            ServerProperty.setClientDBProp(location);
        Logger.getLogger(DBConfigController.class.getName()).log(Level.INFO, "CLIENT DB FILE : {0}",ServerProperty.getClientDBFileProp());
        ServerProperty.storeProps();
    }


    
}
